using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(AnimFunc))]
public class AnimFuncDrawer : PropertyDrawer
{
    public C_AnimationController controller;

    public SerializedProperty funcNameIndex;
    public SerializedProperty funcName;
    public SerializedProperty hasVariable;
    public SerializedProperty isOfVariableList;
    public SerializedProperty variable;
    public SerializedProperty foldout;

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        using (new EditorGUI.PropertyScope(position, label, property))
        {
            controller = property.serializedObject.targetObject as C_AnimationController;

            funcNameIndex = property.FindPropertyRelative("funcNameIndex");
            funcName = property.FindPropertyRelative("funcName");
            hasVariable = property.FindPropertyRelative("hasVariable");
            isOfVariableList = property.FindPropertyRelative("isOfVariableList");
            variable = property.FindPropertyRelative("variable");
            foldout = property.FindPropertyRelative("foldout");
            //label宽度
            EditorGUIUtility.labelWidth = 50;
            position.height = EditorGUIUtility.singleLineHeight;

            var foldoutRect = new Rect(position)
            {
                width = position.width,
                height = position.height,
            };
            var funcNameRect = new Rect(foldoutRect)
            {
                width = position.width/2.5f,
                y = foldoutRect.y + position.height + 5,
            };
            var hasVariableRect = new Rect(foldoutRect)
            {
                width = position.width / 4,
                x = funcNameRect.x + position.width / 2,
                y = foldoutRect.y + position.height + 5,
            };
            var isOfVariableListRect = new Rect(foldoutRect)
            {
                width = position.width / 4,
                x = funcNameRect.x + position.width / 4*3,
                y = foldoutRect.y + position.height + 5,
            };
            var variableRect = new Rect(funcNameRect)
            {
                width = position.width,
                y = funcNameRect.y + position.height + 5,
            };
            foldout.boolValue = EditorGUI.Foldout(foldoutRect, foldout.boolValue, funcName.stringValue, true);
            if (foldout.boolValue)
            {
                string[] animNames = controller.componentManager.GetCallbackFuncNameList().ToArray();
                funcNameIndex.intValue = EditorGUI.Popup(funcNameRect, "函数名", funcNameIndex.intValue, animNames);
                funcName.stringValue = animNames[funcNameIndex.intValue];
                //EditorGUI.PropertyField(funcNameRect, funcName, new GUIContent("函数名"), true);
                EditorGUI.PropertyField(hasVariableRect, hasVariable, new GUIContent("HasV"), true);
                EditorGUI.PropertyField(isOfVariableListRect, isOfVariableList, new GUIContent("IsVL"), true);
                if(hasVariable.boolValue)
                {
                    EditorGUI.PropertyField(variableRect, variable, new GUIContent("参数"), true);
                }
            }
        }
    }
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        hasVariable = property.FindPropertyRelative("hasVariable");
        foldout = property.FindPropertyRelative("foldout");
        if (!foldout.boolValue)
        {
            return base.GetPropertyHeight(property, label);
        }
        else if(!hasVariable.boolValue)
        {
            return base.GetPropertyHeight(property, label) * 2 + 5 * 1; 
        }
        else
        {
            return base.GetPropertyHeight(property, label) * 3 + 5 * 2;
        }
    }
}
